package com.terrynoya.Resource.lib
{
	import com.terrynoya.Resource.events.ExternalLibraryEvent;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	
	[Event(name="load_complete",type="com.terrynoya.Resource.events.ExternalLibraryEvent")]
	
	[Event(name="io_error",type="com.terrynoya.Resource.events.ExternalLibraryEvent")]
	
    /**
     * 外部库类，存放外部库的信息,path和linkages
     * @author TerryYao
     */
    public class ExternalLibrary extends EventDispatcher
    {
        private var _path:String;

        private var _linkages:Array;//UzLinkage
		
		private var _status:String;
		
        /**
         * 
         * @param path
         */
        public function ExternalLibrary(path:String)
        {
            this._path = path;
            this._linkages = [];
            this._status = LibraryStatus.READY;
        }
		
		/**
		 * 加载状态
		 * @see com.terrynoya.Resource.lib.UzLibraryStatus
		 * @return 
		 */
		public function get status():String
		{
			return this._status;
		}
		
		/**
		 * 外部库地址
		 * @return 
		 */
		public function get path():String
		{
			return this._path;
		}
		
        /**
         * 加入一个linkage
         * @param link
         */
        public function addLinkage(link:Linkage):void
        {
            this._linkages.push(link);
        }

        /**
         * 查找linkage返回一个查找句柄
         * @see com.terrynoya.Resource.lib.UzFindLinkageHandler
         * @param linkageId
         * @return 
         */
        public function findLinkage(linkageId:String):LinkageFoundHandler
        {
        	var index:int = -1;
            for (var i:int = 0; i < this._linkages.length; i++)
            {
            	var lk:Linkage = this._linkages[i];
				if(lk.id == linkageId)
				{
					index = i;
					break;
				}
            }
            var flh:LinkageFoundHandler = new LinkageFoundHandler(this,index)
            return flh;
        }
        
        /**
         * 加载资源，该方法主要供ResourceManager调用
         */
        public function loadResource():void
        {
        	trace("loadResource:"+this.path);
			var ld:Loader = new Loader();
			ld.contentLoaderInfo.addEventListener(Event.COMPLETE, onResourceLoaded);
			ld.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIO_ERROR);
			var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			ld.load(new URLRequest(this._path),ldrContext);
			this._status = LibraryStatus.LOADING;
        }
        
        
        /**
         * Complete事件
         * @param e
         */
        protected function onResourceLoaded(e:Event):void
		{
			var li:LoaderInfo = LoaderInfo(e.currentTarget);
			this._status = LibraryStatus.COMPLETE;
			var evt:ExternalLibraryEvent = new ExternalLibraryEvent(ExternalLibraryEvent.LOAD_COMPLETE);
			this.dispatchEvent(evt);
		}
		
		
		 /**
         * IO错误事件
         * @param e
         */
		private function onIO_ERROR(e:IOErrorEvent):void
		{
			this._status = LibraryStatus.IO_ERROR;
			var evt:ExternalLibraryEvent = new ExternalLibraryEvent(ExternalLibraryEvent.IO_ERROR);
			this.dispatchEvent(evt);
		}
    }
}